package jp.ac.waseda.cs.washi.game.ai;

// PlayerAction.LEFT を LEFT と記述できるようにする宣言
import static jp.ac.waseda.cs.washi.game.api.PlayerAction.*;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import jp.ac.waseda.cs.washi.game.api.CharaInfo;
import jp.ac.waseda.cs.washi.game.api.GameInfo;
import jp.ac.waseda.cs.washi.game.api.MapInfo;
import jp.ac.waseda.cs.washi.game.api.Player;
import jp.ac.waseda.cs.washi.game.api.PlayerAction;
import jp.ac.waseda.cs.washi.game.api.PumpkinInfo;
import jp.ac.waseda.cs.washi.game.entity.PumpkinCore;
import jp.ac.waseda.cs.washi.gameaiarena.api.Direction4;
import jp.ac.waseda.cs.washi.gameaiarena.api.Point2;

public class EnemyPumpkinDestroyer implements Player {
	private static CharaInfo player;
	private static CharaInfo enemy;
	private static MapInfo map;
	private static LinkedList<PlayerAction> commandList = new LinkedList<PlayerAction>();
	private static Direction4[] directions;
	private Random random;

	@Override
	public void initialize(GameInfo env) {
		map = env.getMap();
		player = env.getPlayer();
		enemy = env.getEnemy();
		random = new Random();
	}

	@Override
	public PlayerAction execute(GameInfo gameInfo) {
		if (commandList.size() == 0) {
			ArrayList<PumpkinInfo> pump = enemy.getPumpkins();
			if (pump.size() == 0) {
				// 上下左右移動，もしくは，魔方陣の開発をランダムで選択
				int iAction = (int) random.nextInt(5);
				return PlayerAction.values()[iAction];
			} else {
				List<PumpkinCore> cores = pump.get(0).getCores();
				if (cores.size() == 0)
					return NONE;
				else {
					PumpkinCore core = cores.get(0);
					Point2 p = core.getLocation();
					int strength = core.getStrength();
					moveTo(p);
					for (int i = 0; i < strength; i++)
						commandList.add(SPELL);
				}
			}
		}
		return commandList.remove(0);
	}

	public static void moveTo(Point2 p) {
		directions = map.getPath(player.getLocation(), p);
		for (Direction4 dir : directions)
			commandList.add(PlayerAction.fromDirection(dir));
		return;
	}
}
